package cn.roadtrip.musermodule.spi.role;

import cn.roadtrip.musermodule.bs.UserImpl;
import cn.roadtrip.musermodule.dao.userlayout.LeaderInforsMapper;
import cn.roadtrip.musermodule.dao.userlayout.UserBindRoleMapper;
import cn.roadtrip.musermodule.dao.userlayout.po.LeaderInfors;
import cn.roadtrip.musermodule.dao.userlayout.po.UserBindRole;
import cn.roadtrip.umysqlmodule.transaction.TransactionRollback;
import cn.roadtrip.utilmodule.util.DataBean;
import cn.roadtrip.utilmodule.util.enumpck.Enum_UserBindRoleStats;
import cn.roadtrip.utilmodule.util.enumpck.Enum_UserRoleType;
import com.github.yitter.idgen.YitIdHelper;
import com.google.gson.Gson;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;

/**
 * 领队申请
 */
@Component
public class ApplyLeaderRoleImpl implements ApplyRole {

    @Autowired
    private TransactionRollback transactionRollback;

    @Autowired
    private SqlSessionFactory sqlSessionFactory;
    @Override
    public DataBean applyRole(UserImpl.Module_ApplyUserRole model) {
        return transactionRollback.transaction(
                () -> {
                    try (SqlSession sql = sqlSessionFactory.openSession(true)) {
                        UserBindRoleMapper mapper = sql.getMapper(UserBindRoleMapper.class);

                        UserBindRole r = mapper.getUserRoleByType(model.getUserId(), model.getRoleType());
                        if (r != null) {
                            if (r.getStats().equals(Enum_UserBindRoleStats.Apply.getCode())) {
                                return DataBean.err("apply-err", "申请中");
                            }
                            if (r.getStats().equals(Enum_UserBindRoleStats.Active.getCode())) {
                                return DataBean.err("active-err", "已激活角色，不可申请");
                            }
                            //更新
                            r.setModifytime(System.currentTimeMillis());
                            Gson gson = new Gson();
                            try {
                                r.setRemark(gson.toJson(model.getRemark()));
                            } catch (Exception e) {
                                r.setRemark("{}");
                            }
                            r.setStats(Enum_UserBindRoleStats.Apply.getCode());
                            mapper.updateByPrimaryKeySelective(r);
                        } else {
                            //插入新得角色
                            r = new UserBindRole(
                                    YitIdHelper.nextId() + "",
                                    false,
                                    Enum_UserBindRoleStats.Apply.getCode(),
                                    model.getUserId(),
                                    model.getRoleType(),
                                    System.currentTimeMillis(),
                                    System.currentTimeMillis(),
                                    "{}"
                            );
                            Gson gson = new Gson();
                            try {
                                r.setRemark(gson.toJson(model.getRemark()));
                            } catch (Exception e) {
                                r.setRemark("{}");
                            }
                            mapper.insert(r);
                        }
                        //插入领队角色信息
                        LeaderInforsMapper leaderInforsMapper = sql.getMapper(LeaderInforsMapper.class);
                        java.util.Map<String,String> extInfors = new HashMap<>();
                        extInfors.put("wchatnum",(String) model.getRemark().get("wchatnum") );
                        extInfors.put("emergencyContact",(String) model.getRemark().get("emergencyContact") );
                        extInfors.put("emergencyPhone",(String) model.getRemark().get("emergencyPhone") );
                        extInfors.put("college",(String) model.getRemark().get("college") );
                        extInfors.put("address",(String) model.getRemark().get("address") );
                        extInfors.put("idCardA",(String) model.getRemark().get("idCardA") );
                        extInfors.put("idCardB",(String) model.getRemark().get("idCardB") );
                        extInfors.put("guideCertificate",(String) model.getRemark().get("guideCertificate") );
                        extInfors.put("aboutLeader",(String) model.getRemark().get("aboutLeader") );
                        extInfors.put("aboutTrip",(String) model.getRemark().get("aboutTrip") );
                        extInfors.put("aboutRout",(String) model.getRemark().get("aboutRout") );
                        extInfors.put("aboutExp",(String) model.getRemark().get("aboutExp") );
                        extInfors.put("selfEval",(String) model.getRemark().get("selfEval") );
                        Gson gson = new Gson();
                        leaderInforsMapper.insert(
                                new LeaderInfors(
                                        YitIdHelper.nextId() + "",
                                        Enum_UserBindRoleStats.Apply.getCode(),
                                        model.getUserId(),
                                        1,
                                        (String) model.getRemark().get("realName"),
                                        (String) model.getRemark().get("codeName"),
                                        (String) model.getRemark().get("phone"),
                                        (String) model.getRemark().get("sex"),
                                        (String) model.getRemark().get("idNum"),
                                        (String) model.getRemark().get("guidNum"),
                                        (String) model.getRemark().get("school"),
                                        (String) model.getRemark().get("email"),
                                        gson.toJson(extInfors)
                                )
                        );
                        sql.commit();
                        DataBean db = DataBean.suc(true, 1, "suc", "操作成功");
                        return db;
                    }
                },
                (e) -> {
                    return DataBean.err("trans-err", e.getMessage());
                }
        );
    }

    @Override
    public boolean supports(String delimiter) {
        return delimiter.equals(Enum_UserRoleType.Leader.getCode());
    }
}
